package com.jhgsys.internal.hazard.controller;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jhgsys.internal.base.service.IBaseCautionaryLessonService;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.constant.HazardConstant;
import com.jhgsys.internal.hazard.dto.HazardReportDto;
import com.jhgsys.internal.hazard.service.IHazardReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author wanghw
 * @CreateTime 2024/8/21 15:59
 * @Description 隐患清单
 * @Version 1.0
 */
@Slf4j
@Validated
@Controller
public class hazardListController extends BaseController {
    @Autowired
    private IHazardReportService hazardReportService;

    @Autowired
    private IBaseCautionaryLessonService baseCautionaryLessonService;


    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardList")
    @RequiresPermissions("hazardList:view")
    public String hazardListIndex(Model model){
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("hazard/hazardList/hazardList");
    }



    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardList/detail/{id}")
    @RequiresPermissions("hazardList:view")
    public String hazardReportDetail(Model model, @PathVariable("id") String id) throws JsonProcessingException {
        HazardReportDto dto = hazardReportService.findHazardReportById(id);
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("hazardReport", JSON.parse(new ObjectMapper().writeValueAsString(dto)));
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("hazard/hazardList/hazardDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardList/detailForLesson/{id}/{lessonId}")
    @RequiresPermissions("hazardList:view")
    public String hazardReportDetailForLesson(Model model, @PathVariable("id") String id, @PathVariable("lessonId") String lessonId) throws JsonProcessingException {
        HazardReportDto dto = hazardReportService.findHazardReportById(id);
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("hazardReport", JSON.parse(new ObjectMapper().writeValueAsString(dto)));
        model.addAttribute("baseCautionaryLesson", baseCautionaryLessonService.findBaseCautionaryLessonById(lessonId));
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("base/baseCautionaryLesson/hazardDetailForLesson");
    }




    /**
     * 隐患清单查询（分页）
     * @author wanghw
     * @date 2024-08-20 09:23:04
     */
    @GetMapping("hazardList/list")
    @ResponseBody
    @RequiresPermissions("hazardList:view")
    public FebsResponse hazardReportList(QueryRequest request, HazardReportDto hazardReport) {
        hazardReport.setStatus(HazardConstant.HAZARD_STATUS.ISSUANCED);
        Map<String, Object> dataTable = getDataTable(this.hazardReportService.findHazardReportPage(request, hazardReport));
        return new FebsResponse().success().data(dataTable);
    }



}
